package ims.servlet;

import ims.beans.Product;
import ims.constants.IMSConstants;
import ims.db.update.product.UpdateProductDAO;
import ims.db.update.product.UpdateProductDbRequest;
import ims.db.update.product.UpdateProductDbResponse;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class UpdateProductServlet
 */
@WebServlet("/UpdateProductServlet")
public class UpdateProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateProductServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			response.setContentType("text/html;charset=UTF-8");
	
			//Retrieve all product types
			UpdateProductDbRequest dbRequest = new UpdateProductDbRequest();
			Product product = new Product();
			product.setPid(request.getParameter("prodId"));
			product.setPrice(Double.parseDouble(request.getParameter("price")));
			product.setProdDesc(request.getParameter("prodNm"));
			product.setProdTypeId(request.getParameter("prodType"));
			
			
			dbRequest.setProduct(product);
			UpdateProductDAO db = new UpdateProductDAO();
			UpdateProductDbResponse dbResponse = (UpdateProductDbResponse) db.execute(dbRequest);
			
			String nextLoc = "/IMS/product?"+IMSConstants.RESPONSE_HEADER+"="+dbResponse.getMsg();      
			response.sendRedirect(response.encodeRedirectURL(nextLoc));

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}
